﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Text.RegularExpressions;

//Required for custom paging
using System.Linq.Expressions;

namespace Mvc4_CRUD_jQueryDialog.Models
{
    public class ModelServices : IDisposable
    {
        private readonly Entities entities = new Entities();

        public IEnumerable<Customer> GetCustomer()
        {
            return entities.Customers.ToList();
        }

        //For Custom Paging

        public IEnumerable<Customer> GetCustomerPage(int pageNumber, int pageSize, string searchCriteria)
        {
            if (pageNumber < 1)
                pageNumber = 1;

            return entities.Customers
              .OrderBy(customer => customer.CustID)
              .Skip((pageNumber - 1) * pageSize)
              .Take(pageSize)
              .ToList();
        }
        public int CountCustomer()
        {
            return 1;
        }

        public void Dispose()
        {
            entities.Dispose();
        }

        //For Edit 
        public Customer GetCustomer(int mCustID)
        {
            return entities.Customers.Where(m => m.CustID == mCustID).FirstOrDefault();
        }

        public bool CreateCustomer(Customer mCustomer)
        {
            try
            {
                entities.Customers.AddOrUpdate(mCustomer);
                entities.SaveChanges();
                return true;
            }
            catch (Exception mex)
            {
                return false;
            }
        }

        public bool UpdateCustomer(Customer mCustomer)
        {
            try
            {
                Customer data = entities.Customers.Where(m => m.CustID == mCustomer.CustID).FirstOrDefault();

                data.Name = mCustomer.Name;
                data.Address = mCustomer.Address;
                data.ContactNo = mCustomer.ContactNo;
                entities.SaveChanges();
                return true;
            }
            catch (Exception mex)
            {
                return false;
            }
        }

        public bool DeleteCustomer(int mCustID)
        {
            try
            {
                Customer data = entities.Customers.Where(m => m.CustID == mCustID).FirstOrDefault();
                entities.Customers.Remove(data);
                entities.SaveChanges();
                return true;
            }
            catch (Exception mex)
            {
                return false;
            }
        }
    }

    //public enum SortDirection
    //{
    //    Ascending,
    //    Descencing
    //}

    public class PagedCustomerModel
    {
        public int TotalRows { get; set; }
        public IEnumerable<Customer> Customer { get; set; }
        public int PageSize { get; set; }
    }

    public class CustomerModel
    {

        [ScaffoldColumn(false)]
        public int CustID { get; set; }

        [Required(ErrorMessage = "Please Enter Name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please Enter Address")]
        [UIHint("Multiline")]
        public string Address { get; set; }

        [Required(ErrorMessage = "Please Enter Contact No")]
        public string ContactNo { get; set; }
    }
}